/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.activity;

import exception.ExistException;
import java.io.Serializable;
import java.sql.Time;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import session.stateless.ActivitySessionBean;
import session.stateless.SystemMessagesBean;
import util.entityhelper.AttractionState;
import util.enumeration.ActivityType;
import util.enumeration.SingaporeArea;
import util.enumeration.Time24H;
import util.localization.WebLocalization;

/**
 *
 * @author AdminNUS
 */
@ManagedBean
@RequestScoped
public class ActivityCreateAttractionManagedBean implements Serializable {
    
    @EJB
    private ActivitySessionBean activityMgr;
    @EJB
    private SystemMessagesBean systemMessages;
    private FacesContext ctx = FacesContext.getCurrentInstance();
    private WebLocalization localization;
    // use entity helpers to extract data
    private AttractionState attractionState;
    //  user preferred locale
    private String preferredLocale;
    private ActivityType activityType;
    private String type;
    private SingaporeArea singaporeArea;    
    private String activityName;
    private String activityDesc;
    private Time24H openingHour;
    private Time24H closingHour;
    private String imgUrl;
    private int postalCode;
    private String unitNumber;
    private String blkNumber;
    private String streetName;
    private String buildingName;

    /** Creates a new instance of ActivityCreateAttractionManagedBean */
    public ActivityCreateAttractionManagedBean() {
        /* LOCALIZATION */
        localization = new WebLocalization();
        preferredLocale = localization.getPreferredLocaleFromSession();
        /* END LOCALIZATION */
    }
    
    public void createAttraction(ActionEvent event) {
        activityMgr.setSelectedLocale(preferredLocale);
        try {
            System.out.println("create attraction...");
            System.out.println(activityType);
            System.out.println(openingHour.getTime());
            System.out.println(closingHour);
            System.out.println(activityName);
            System.out.println(activityDesc);
            Calendar opening = new GregorianCalendar();
            opening.set(Calendar.MINUTE, 0);
            opening.set(Calendar.SECOND, 0);
            opening.set(Calendar.HOUR_OF_DAY, openingHour.getTime());
            Time o = new Time(opening.getTimeInMillis());
            Calendar closing = new GregorianCalendar();
            closing.set(Calendar.MINUTE, 0);
            closing.set(Calendar.SECOND, 0);
            closing.set(Calendar.HOUR_OF_DAY, closingHour.getTime());
            Time c = new Time(closing.getTimeInMillis());
            
            System.out.println(postalCode + " " + " " + blkNumber + " " + unitNumber + " " + streetName + " " + buildingName);
            activityMgr.createAttraction(activityType, SingaporeArea.CENTRAL, activityName, activityDesc, o, c, imgUrl, postalCode, blkNumber, unitNumber, streetName, buildingName);

            // print success message
            String statusMessage = systemMessages.getSystemMessage("ATTRACTION_CREATION_SUCCESS", preferredLocale);
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, statusMessage, ""));
            
            reset();
            
        } catch (ExistException ex) {
            // display exception
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
            System.out.println("caught 2");
        } catch (Exception ex) {
            // display exception tp user
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, ex.getMessage(), ""));
            // backend message for Tcube
            ex.printStackTrace();
        }
    }
    
    private void reset() {
        activityType = null;
        activityName = "";
        activityDesc = "";
        singaporeArea = null;
        openingHour = null;
        closingHour = null;
        imgUrl = "";
        blkNumber = "";
        streetName = "";
        buildingName = "";
        unitNumber = "";
        //postalCode = "";
    }
    
    public AttractionState getAttractionState() {
        return attractionState;
    }
    
    public void setAttractionState(AttractionState attractionState) {
        this.attractionState = attractionState;
    }
    
    public ActivityType getActivityType() {
        return activityType;
    }
    
    public void setActivityType(ActivityType activityType) {
        this.activityType = activityType;
    }
    
    public String getActivityName() {
        return activityName;
    }
    
    public void setActivityName(String activityName) {
        this.activityName = activityName;
    }
    
    public String getActivityDesc() {
        return activityDesc;
    }
    
    public void setActivityDesc(String activityDesc) {
        this.activityDesc = activityDesc;
    }
    
    public Time24H getOpeningHour() {
        return openingHour;
    }
    
    public void setOpeningHour(Time24H openingHour) {
        this.openingHour = openingHour;
    }
    
    public Time24H getClosingHour() {
        return closingHour;
    }
    
    public void setClosingHour(Time24H closingHour) {
        this.closingHour = closingHour;
    }
    
    public String getImgUrl() {
        return imgUrl;
    }
    
    public void setImgUrl(String imgUrl) {
        this.imgUrl = imgUrl;
    }
    
    public int getPostalCode() {
        return postalCode;
    }
    
    public void setPostalCode(int postalCode) {
        this.postalCode = postalCode;
    }
    
    public String getUnitNumber() {
        return unitNumber;
    }
    
    public void setUnitNumber(String unitNumber) {
        this.unitNumber = unitNumber;
    }
    
    public String getBlkNumber() {
        return blkNumber;
    }
    
    public void setBlkNumber(String blkNumber) {
        this.blkNumber = blkNumber;
    }
    
    public String getStreetName() {
        return streetName;
    }
    
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }
    
    public String getBuildingName() {
        return buildingName;
    }
    
    public void setBuildingName(String buildingName) {
        this.buildingName = buildingName;
    }
    
    public SingaporeArea getSingaporeArea() {
        return singaporeArea;
    }
    
    public void setSingaporeArea(SingaporeArea singaporeArea) {
        this.singaporeArea = singaporeArea;
    }
}
